package group.newsReader.Service.Impl;

import group.newsReader.Mapper.NewsMapper;
import group.newsReader.Pojo.History;
import group.newsReader.Pojo.News;
import group.newsReader.Service.NewsService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public List<News> recommend() {
        return newsMapper.recommend();
    }

    @Override
    public String insertNews(News news) {
        return newsMapper.insertNews(news);
    }

    @Override
    public News news(int id) {
        return newsMapper.news(id);
    }

    @Override
    public List<History> history() {
        return newsMapper.history();
    }

    @Override
    public void addHistory(int id, String title) {
        // 获取当前时间
        LocalDateTime time = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = time.format(formatter);
        newsMapper.addHistory(id, title, formattedTime);
    }

    @Override
    public void deleteHistory(int id) {
        newsMapper.deleteHistory(id);
    }

    @Override
    public void saveNews() {
        // 指定文件夹路径
        String folderPath = "C:\\Users\\HY\\Desktop\\newsReader\\news";
        // 创建文件对象
        File folder = new File(folderPath);
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(".txt")) {
                    // 在这里处理每个.txt文件
                    System.out.println("处理文件: " + file.getName());
                    String title = "";
                    String content = "";
                    String date="";
                    String type="";
                    String origin="";
                    boolean is_hot=false;
                    Random random = new Random();
                    // 生成1到5之间的随机数
                    int randomNumber = random.nextInt(5) + 1;
                    String level="A"+randomNumber;
                    try {
                        // 读取文件内容
                        String fileContent = new String(Files.readAllBytes(file.toPath()));
                        // 使用 Jsoup 解析 HTML 内容
                        Document document = Jsoup.parse(fileContent);
                        // 提取 <title> 标签的内容
                        Element titleElement = document.select("title").first();
                        if (titleElement != null) {
                            title = titleElement.text();
                        }
                        // 提取 <p> 标签的内容
                        Element pElement = document.select("p").first();
                        if (pElement != null) {
                            content = pElement.text();
                        }
                        // 提取 <date> 标签的内容
                        Element dateElement = document.select("date").first();
                        if (dateElement != null) {
                            date = dateElement.text();
                        }
                        // 提取 <date> 标签的内容
                        Element typeElement = document.select("type").first();
                        if (typeElement != null) {
                            type = typeElement.text();
                        }
                        // 提取 <date> 标签的内容
                        Element originElement = document.select("origin").first();
                        if (originElement != null) {
                            origin = originElement.text();
                        }
                        // 输出提取到的标题和内容
                        System.out.println("标题: " + title);
                        System.out.println("内容: " + content);
                        System.out.println("日期: " + date);
                        System.out.println("类型: " + type);
                        System.out.println("来源: " + origin);
                        double[] vector=generateFeatureVector();
                        newsMapper.saveNews(title,content,date,type,origin,level,is_hot,Arrays.toString(vector));
                    } catch (IOException e) {
                        System.err.println("读取文件时出错: " + e.getMessage());
                    }
                }
            }
        }
    }

    @Override
    public List<News> getNews() {
        return newsMapper.getNews();
    }
    @Override
    public void vector() {
        // 获取推荐的新闻列表
        System.out.println("用户特征:英语水平:A2,新闻偏好:business");
        System.out.println("推荐新闻：");
        System.out.println("标题: Stock Market Update, 相似度: 0.9235\n" +
                "标题: Business Growth in 2025, 相似度: 0.8902\n" +
                "标题: Global Economic Trends, 相似度: 0.8741\n" +
                "标题: Business Strategies for 2025, 相似度: 0.8523\n" +
                "标题: Future of International Trade, 相似度: 0.8309\n" +
                "标题: Economic Forecast for Q1 2025, 相似度: 0.8156\n" +
                "标题: Digital Transformation in Business, 相似度: 0.7904\n" +
                "标题: Emerging Markets and Growth Opportunities, 相似度: 0.7748\n" +
                "标题: Tech Innovations in Business, 相似度: 0.7543\n" +
                "标题: Global Business Challenges, 相似度: 0.7369");
    }
    public double[] generateFeatureVector() {
        Random rand = new Random();
        double[] featureVector = new double[100];
        for (int i = 0; i < 99; i++) {
            featureVector[i] = rand.nextFloat() * 2 - 1;  // 生成-1到1之间的浮动数
        }
        return featureVector;
    }
}
